package com.wx.nbregisterserver.listener;



import com.wx.nbregisterserver.job.HeartBeatJob;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.stereotype.Component;



/**
 * 发送心跳任务触发器
 */
@Component
@DependsOn("heartBeatJob")
public class ClientStatusListener implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {

//        // 绑定定时任务
//        JobDetail jobDetail = jobDetailFactoryBean.getObject();
//
//
//        // 设定触发器执行时间
//
//        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("HeartBeatJob", "send").startNow()
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
//                        .withIntervalInSeconds(2)  // 执行时间间隔
//                        .repeatForever()      // 一直执行
//                ).build();
//
//
//        // 创建调度者工厂
//
//        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
//
//        // 创建调度者
//
//        Scheduler scheduler = stdSchedulerFactory.getScheduler();
//
//        // 启动调度器
//
//        scheduler.start();
//
//        // 设置调度任务
//
//        scheduler.scheduleJob(jobDetail,trigger);













      /*  //连通测试失败计数器
        final ArrayList<Integer> countList=new ArrayList<>();
        countList.add(0);
       // 获得所有注册信息
        Timer timer = new Timer();
        // 3s拉取一次信息
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                HashMap<String, ServerInfomation> allRegisterMesg = regesterServer.getAllRegisterMesg();
                registMesgContext.putMesg(allRegisterMesg);
            }
        },1000,3000);

        //获得所有注册的信息
        final ArrayList<  HashMap<String, ServerInfomation>> mapList=new ArrayList<>();

        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                mapList.add(0,registMesgContext.getMesg());
            }
        },1000,4000);



        // 循环遍历每个节点发送心跳
        HashMap<String, ServerInfomation> mesg = mapList.get(0);

        for (Map.Entry<String, ServerInfomation> stringServerInfomationEntry : mesg.entrySet()) {

                ServerInfomation value = stringServerInfomationEntry.getValue();
                String serverName = value.getServerName();
                timer.scheduleAtFixedRate(new TimerTask() {
                    @Override
                    public void run() {
                        int port = value.getPort();
                        String ip = value.getIp();
                        boolean severStatus = httpHeartBeatStatus.getSeverStatus(ip, port);
                        // 判断心跳状态
                        if(severStatus){
                            // 正常心跳可用
                            Integer integer = countList.get(0);
                            if(integer>0){
                                integer--;
                                countList.set(0,integer);
                            }
                            // 从上下文中获取数据
                            HashMap<String, ServerInfomation> mesg = registMesgContext.getMesg();
                            // 获得对应的注册信息
                            ServerInfomation serverInfomation = mesg.get(serverName);
                            // 把状态设置为不可用
                            serverInfomation.setStatus(true);
                            // 重新添加到容器中
                            mesg.put(serverName,serverInfomation);
                            // 重新添加到上下文中
                            registMesgContext.putMesg(mesg);
                            System.out.println("+++++"+serverName+"+++"+"可用"+"++++");

                        }else {
                            Integer integer = countList.get(0);
                            if(integer>4){
                                // 从上下文中获取数据
                                HashMap<String, ServerInfomation> mesg = registMesgContext.getMesg();
                                // 获得对应的注册信息
                                ServerInfomation serverInfomation = mesg.get(serverName);
                                // 把状态设置为不可用
                                serverInfomation.setStatus(false);
                                // 重新添加到容器中
                                mesg.put(serverName,serverInfomation);
                                // 重新添加到上下文中
                                registMesgContext.putMesg(mesg);
                                // 提示服务不可用
                                System.out.println(serverName+"挂了"+"ip:"+serverInfomation.getIp()+"port:"+serverInfomation.getPort());
                            }else {
                                integer++;
                                countList.set(0,integer);
                            }
                        }


                    }
                },3000,3000);
            }*/

       /* //连通测试失败计数器
        final ArrayList<Integer> countList=new ArrayList<>();
        countList.add(0);

        Timer timer1 = new Timer();
        Timer timer2 = new Timer();
        timer1.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                // 获得注册的所有信息
                HashMap<String, ServerInfomation> allRegisterMesg = regesterServer.getAllRegisterMesg();
                if(allRegisterMesg!=null){
                    *//**
                     * 遍历集合中的所有注册信息
                     * 集合中信息为 服务名：ip=端口号
                     *//*
                    Set<Map.Entry<String, ServerInfomation>> entries = allRegisterMesg.entrySet();

                    for (Map.Entry<String, ServerInfomation> entry : entries) {
                        // 获得服务名
                        String serverName = entry.getKey();
                        // 获得ip地址与端口号
                        ServerInfomation registerMesg = entry.getValue();

                        String ip = registerMesg.getIp();
                        Integer port=registerMesg.getPort();
                        // 定时任务
                        timer2.scheduleAtFixedRate(new TimerTask() {
                            @Override
                            public void run() {
                                boolean status = httpHeartBeatStatus.getSeverStatus(ip, port);
                                if(status){
                                    // ping通了，服务可用
                                    Integer integer = countList.get(0);
                                    if(integer>0){
                                        integer--;
                                        countList.set(0,integer);
                                    }

                                    // 从上下文中获取数据
                                    HashMap<String, ServerInfomation> mesg = registMesgContext.getMesg();
                                    // 获得对应的注册信息
                                    ServerInfomation serverInfomation = mesg.get(serverName);
                                    // 把状态设置为不可用
                                    serverInfomation.setStatus(true);
                                    // 重新添加到容器中
                                    mesg.put(serverName,serverInfomation);
                                    // 重新添加到上下文中
                                    registMesgContext.putMesg(mesg);
                                    System.out.println("+++++"+serverName+"+++"+"可用"+"++++");
                                }else {
                                    // 未ping通过，服务不可用
                                    Integer integer = countList.get(0);
                                    if(integer>4){
                                        // 从上下文中获取数据
                                        HashMap<String, ServerInfomation> mesg = registMesgContext.getMesg();
                                        // 获得对应的注册信息
                                        ServerInfomation serverInfomation = mesg.get(serverName);
                                        // 把状态设置为不可用
                                        serverInfomation.setStatus(false);
                                        // 重新添加到容器中
                                        mesg.put(serverName,serverInfomation);
                                        // 重新添加到上下文中
                                        registMesgContext.putMesg(mesg);

                                        // 提示服务不可用
                                        System.out.println(serverName+"挂了"+"ip:"+serverInfomation.getIp()+"port:"+serverInfomation.getPort());
                                    }else {
                                        integer++;
                                        countList.set(0,integer);
                                    }
                                }
                            }
                        },1000,3000);
                    }
                }

            }
        },1000,1000);*/


    }
}
